package com.ishopping.web.manage.controller;

import com.github.pagehelper.PageInfo;
import com.ishopping.api.product.entity.Product;
import com.ishopping.api.product.service.IProductService;
import com.ishopping.common.core.base.constant.CommonConstants;
import com.ishopping.common.core.base.controller.BaseController;
import com.ishopping.common.core.base.enums.ReturnEnum;
import com.ishopping.common.core.vo.DataGridResult;
import com.ishopping.common.core.vo.JsonResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 产品controller
 */
@Controller
@RequestMapping("/product/item")
public class ProductController extends BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductController.class);

    @Autowired
    private IProductService productService;

    /**
     * 默认页面
     */
    @RequiresPermissions("product:item:manage")
    @RequestMapping(value = "/manage", method = RequestMethod.GET)
    public String productManager() {
        return "product/productMgt";
    }

    /**
     * 获取产品列表
     */
    @RequiresPermissions("product:item:manage")
    @RequestMapping(value="/list",method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<DataGridResult> productList(HttpServletRequest request, Product product) {
        try {
            int currentPage = Integer.valueOf(request.getParameter("page"));
            int pageSize = Integer.valueOf(request.getParameter("rows"));
            PageInfo page = productService.queryPageListByWhere(product, currentPage, pageSize);
            DataGridResult dataGrid = new DataGridResult(page.getTotal(), page.getList());
            return ResponseEntity.ok(dataGrid);
        } catch (Exception e) {
            LOGGER.error("查询产品列表出错!" , e);
        }
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
    }

    /**
     * 添加产品跳转
     * @param model
     */
    @RequiresPermissions("product:item:add")
    @RequestMapping(value = "/addForm", method = RequestMethod.GET)
    public String addProductForm(Model model) {
        model.addAttribute("product", new Product());
        model.addAttribute("action", CommonConstants.ADD);
        return "product/productForm";
    }

    /**
     * 添加产品
     * @param product
     */
    @RequiresPermissions("product:item:add")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Object addProduct(Product product) {
        int result = productService.save(product);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 修改产品跳转
     * @param id
     * @param model
     * @return String
     */
    @RequiresPermissions("product:item:update")
    @RequestMapping(value = "/updateForm/{id}", method = RequestMethod.GET)
    public String updateProductForm(@PathVariable("id") Long id, Model model) {
        model.addAttribute("product", productService.queryById(id));
        model.addAttribute("action", CommonConstants.UPDATE);
        return "product/productForm";
    }

    /**
     * 修改产品
     * @param product
     * @return Object
     */
    @RequiresPermissions("product:item:update")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ResponseBody
    public Object updateProduct(Product product) {
        int result = productService.updateSelective(product);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 删除产品
     * @param id
     * @return Object
     */
    @RequiresPermissions("product:item:delete")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Object deleteProduct(@PathVariable("id") Long id) {
        int result = productService.deleteById(id);
        if(result > 0) {
            return JsonResult.success();
        }else{
            return JsonResult.build(ReturnEnum.USER_FAIL.getValue(), ReturnEnum.USER_FAIL.getText());
        }
    }

    /**
     * 上架产品
     * @param model
     * @param id
     * @return
     */
    @RequiresPermissions("product:item:shelve")
    @RequestMapping(value = "/{id}/shelve")
    public String shelveProduct(Model model, @PathVariable("id") Long id) {
        //TODO
        return "product/productMgt";
    }


    /**
     * 下架产品
     * @param model
     * @param id
     * @return
     */
    @RequiresPermissions("product:item:unshelve")
    @RequestMapping(value = "/{id}/unshelve")
    public String unshelveProduct(Model model, @PathVariable("id") Long id) {
        //TODO
        return "product/productMgt";
    }

}
